package cn.doways.api.controller;

import cn.doways.api.dto.*;
import cn.doways.api.entity.Student;
import cn.doways.api.entity.StudentCollegeDetail;
import cn.doways.api.service.serviceimpl.TransactionServiceImpl;
import cn.doways.api.service.serviceimpl.UsersServiceImpl;
import cn.doways.api.vo.LateListVO;
import cn.doways.api.vo.PersonalCourseTableVO;
import cn.doways.api.vo.SchoolTimeListVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName UsersController
 * @Description //TODO
 * @Author shellming
 * @Date 2019-08-16 20:05
 **/
@RestController
public class UsersController {

    private SignDTO signDTO;
    private UsersServiceImpl usersService;
    private TransactionServiceImpl transactionService;

    @Autowired
    public UsersController(UsersServiceImpl usersService, TransactionServiceImpl transactionService,SignDTO signDTO) {
        this.signDTO=signDTO;
        this.usersService = usersService;
        this.transactionService = transactionService;
    }

    @GetMapping("/users/hello")
    public String testController(){
        return "<h1>HelloWord</h1>";
    }

    @PreAuthorize("hasRole('STUDENT') or hasRole('TEACHER')")
    @GetMapping("/users/{phoneNumber}")
    @ApiOperation(value = "获取学生的个人信息",tags = "用户",notes = "有200，404，401，403四种状态")
    public Result<Userinfo<StudentCollegeDetail>> getStudentInfo( @PathVariable String phoneNumber){
            if (usersService.getStudentInfo(phoneNumber)==null){
                return Result.error("查询异常");
            }else {
                return Result.success(usersService.getStudentInfo(phoneNumber));
            }
    }


    @PreAuthorize("hasRole('STUDENT') or hasRole('TEACHER')")
    @PatchMapping("/users/{id}/intro")
    @ApiOperation(value = "修改学生自我介绍",tags = "用户",notes = "有200，404，401，403四种状态")
    public Result<Integer> updateUserIntro(@PathVariable String id, @RequestBody Userinfo userinfo){

        if(usersService.updateIntroduction(id,userinfo.getIntroduction())>0){
            return Result.success(1);
        }else{
            return Result.error(0);
        }

    }


    @PreAuthorize("hasRole('STUDENT') or hasRole('TEACHER')")
    @PatchMapping("/users/{id}/avatar")
    @ApiOperation(value = "修改学生头像",tags = "用户",notes = "有200，404，401，403四种状态")
    public Result<Integer> updateAvatar(@PathVariable String id, @RequestBody Student student){
        if (StringUtils.isEmpty(student.getAvatar())){
            return Result.error("错误的请求");
        }else if(usersService.updateAvatar(id,student.getAvatar())>0){
            return Result.success(1);
        }else {
            return Result.error(0);
        }
    }


    @PreAuthorize("hasRole('STUDENT')")
    @GetMapping("/users/{id}/course")
    @ApiOperation(value = "获取学生当日课表",tags = "用户",notes = "有200，404，401，403四种状态")
    public Result<List<PersonalCourseTableVO>> getPersonalCourseTable(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return Result.error("无效的ID！");
        }else if (usersService.getPersonalCourseTable(id)!=null){
            return Result.success(usersService.getPersonalCourseTable(id));
        }else {
            return Result.error("查询出错！");
        }

    }

    @PreAuthorize("hasRole('STUDENT')")
    @GetMapping("/users/{id}/termtime/{yearOne}/to/{yearTwo}/{term}")
    @ApiOperation(value = "获取校历",tags = "用户",notes = "有200，404，401，403四种状态")
    public Result<List<SchoolTimeListVO>> getTermTable(@PathVariable String id,
                                                       @PathVariable int yearOne,
                                                       @PathVariable int yearTwo,
                                                       @PathVariable int term){
        if (StringUtils.isEmpty(id)){
            return Result.error("无效的ID！");
        }else if(usersService.getWeekTimeList(id,yearOne,yearTwo,term)!=null){
            return Result.success(usersService.getWeekTimeList(id,yearOne,yearTwo,term));
        }else {
            return Result.error("查询出错！");
        }
    }

    @PreAuthorize("hasRole('STUDENT')")
    @PostMapping("/users/sign")
    @ApiOperation(value = "签到",tags = "事务",notes = "有200，404，401，403四种状态")
    public Result<String> signCourse(@RequestBody SignDTO signDTO){
        return transactionService.signTransaction(signDTO);
    }

    @PreAuthorize("hasRole('STUDENT')")
    @GetMapping("/users/sign")
    @ApiOperation(value = "检查签到",tags = "事务",notes = "有200，404，401，403四种状态")
    public Result<String> signCheck(@RequestParam(value = "buildingUuid") String buildingUuid,
                                     @RequestParam(value = "floorsMajor") int floorsMajor,
                                     @RequestParam(value = "id") String id,
                                     @RequestParam(value = "roomMinor") int roomMinor,
                                     @RequestParam(value = "timeId") int timeId){
        signDTO.setBuildingUuid(buildingUuid);
        signDTO.setFloorsMajor(floorsMajor);
        signDTO.setId(id);
        signDTO.setRoomMinor(roomMinor);
        signDTO.setTimeId(timeId);
        return transactionService.signCheckTransaction(signDTO);
    }


    @PreAuthorize("hasRole('STUDENT')")
    @GetMapping("/users/{id}/{date}/classes/id")
    @ApiOperation(value = "获得当天ClassId",tags = "事务",notes = "有200，404，401，403四种状态")
    public Result<List<String>> getTodayClassId(@PathVariable String id,@PathVariable String date ){
        return transactionService.getTodayClassId(id,date);
    }

    @PreAuthorize("hasRole('STUDENT')")
    @PostMapping("/users/leave/application")
    @ApiOperation(value = "请假",tags = "事务",notes = "有200，404，401，403四种状态")
    public Result<String> doLeave(@RequestBody LeaveRequestDTO leaveRequestDTO){
        return transactionService.doLeaveTransaction(leaveRequestDTO);
    }

    @PreAuthorize("hasRole('STUDENT')")
    @GetMapping("/users/{id}/leave")
    @ApiOperation(value = "查询请假记录",tags = "事务",notes = "有200，404，401，403四种状态")
    public Result<List<LateListVO>> getLateList(@PathVariable String id){
        return transactionService.getLateList(id);
    }

}
